package com.example.chengww.myone.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chengww.myone.R;
import com.example.chengww.myone.anim.FrameAnimation;
import com.example.chengww.myone.bean.Constants;
import com.example.chengww.myone.bean.Data;
import com.example.chengww.myone.bean.Read;
import com.example.chengww.myone.bean.ReadAd;
import com.example.chengww.myone.fragment.HomeFragment;
import com.example.chengww.myone.fragment.MovieFragment;
import com.example.chengww.myone.fragment.MusicFragment;
import com.example.chengww.myone.fragment.ReadFragment;
import com.example.chengww.myone.utils.HTTPUtils;
import com.google.gson.Gson;

import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener ,View.OnClickListener{

    //碎片
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private HomeFragment homeFragment;
    private ReadFragment readFragment;
    private MusicFragment musicFragment;
    private MovieFragment movieFragment;
    private RadioGroup radioGroup;
    //首页数据
    private List<Data> datas;
    private TextView title;
    private ImageView img_search,img_individual_center;
    //加载动画
    private List<ReadAd.Data> readADs;
    private Handler handler ;
    private Read read;
    //再按一次退出提示
    long startTime = 0;
//    private FrameAnimation frameAnimation;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //标题
        title = (TextView) findViewById(R.id.text_title);

        //fragment相关
        fragmentManager = getSupportFragmentManager();
        homeFragment = new HomeFragment();
        readFragment = new ReadFragment();
        musicFragment = new MusicFragment();
        movieFragment = new MovieFragment();

        //开始事务
        transaction = fragmentManager.beginTransaction();
        //添加碎片并隐藏
        transaction.add(R.id.frameLayout,homeFragment).show(homeFragment);
        transaction.add(R.id.frameLayout,readFragment).hide(readFragment);
        transaction.add(R.id.frameLayout,musicFragment).hide(musicFragment);
        transaction.add(R.id.frameLayout,movieFragment).hide(movieFragment);
        transaction.commit();

        //下方导航栏点击事件
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);

        //搜索、个人中心点击监听
        img_search = (ImageView) findViewById(R.id.img_search);
        img_individual_center = (ImageView) findViewById(R.id.img_individual_center);
        img_search.setOnClickListener(this);
        img_individual_center.setOnClickListener(this);

//        frameAnimation = (FrameAnimation) findViewById(R.id.loadAnim);
//        frameAnimation.setVisibility(View.GONE);

    }

    @Override
    public void onCheckedChanged(final RadioGroup group, int checkedId) {
        //开始事务
        transaction = fragmentManager.beginTransaction();
        switch (checkedId){
            //开始事务
            case R.id.rb_home:
                transaction.show(homeFragment).hide(readFragment)
                        .hide(musicFragment).hide(movieFragment);
                title.setBackgroundResource(R.drawable.title_img);
                title.setText("");
                //停止图片轮播
                handler.obtainMessage(10088).sendToTarget();
                //初始化碎片home数据
//                getHomeDatas();
                break;
            case R.id.rb_read:
                transaction.show(readFragment).hide(homeFragment)
                        .hide(musicFragment).hide(movieFragment);
                title.setBackgroundResource(R.color.colorTransparent);
                title.setText("阅读");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //开始加载动画
//                        frameAnimation.setVisibility(View.VISIBLE);
//                        frameAnimation.setBitmapResoursID(Constants.loadAnim);
//                        RelativeLayout relativeLayout = (RelativeLayout)
//                                MainActivity.this.findViewById(R.id.mainLayout);
//                        relativeLayout.addView(frameView);
//                        frameAnimation.set
//                        frameAnimation.start();
                        if (readADs == null || readADs.size() == 0){
                            List<ReadAd.Data> readADs = MainActivity.this.getReadADData();
                        }
                        if (read == null ){
                            String json = HTTPUtils.getStringByInputStream(HTTPUtils.getInputStream(
                                    Constants.READING.READING_CONTENT_PATH));
                            Gson gson = new Gson();
                            read = gson.fromJson(json,Read.class);
                        }
//                        frameAnimation.stop();
//                        frameAnimation.setVisibility(View.GONE);
//                        relativeLayout.removeView(frameView);
                        handler.obtainMessage(10086,readADs).sendToTarget();
                        //开始图片轮播
                        handler.obtainMessage(10087).sendToTarget();
                        //阅读内容
                        handler.obtainMessage(10089,read).sendToTarget();
                    }
                }).start();
                break;
            case R.id.rb_music:
                transaction.show(musicFragment).hide(readFragment)
                        .hide(homeFragment).hide(movieFragment);
                title.setBackgroundResource(R.color.colorTransparent);
                title.setText("音乐");
                //停止图片轮播
                handler.obtainMessage(10088).sendToTarget();
                break;
            case R.id.rb_movie:
                transaction.show(movieFragment).hide(readFragment)
                        .hide(musicFragment).hide(homeFragment);
                title.setBackgroundResource(R.color.colorTransparent);
                title.setText("电影");
                //停止图片轮播
                handler.obtainMessage(10088).sendToTarget();
                break;
        }
        transaction.commit();
    }

    public List<Data> getHomeDatas(){
        if (datas == null || datas.size() == 0){
            Intent intent = getIntent();
            datas = intent.getParcelableArrayListExtra("datas");
        }
        return datas;
    }

    public List<ReadAd.Data> getReadADData(){
        if (readADs == null || readADs.size() == 0){
            InputStream in = HTTPUtils.getInputStream(
                    Constants.READING.READING_AD_PATH);
            String json = HTTPUtils.getStringByInputStream(in);
            Gson gson = new Gson();
            ReadAd readAd = gson.fromJson(json,ReadAd.class);
            readADs = readAd.getData();
        }
        return readADs;
    }

    public void setHandler(Handler handler){
        this.handler = handler;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_search:
                Intent intent = new Intent(this,SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.img_individual_center:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime >= 2000){
            Toast.makeText(this,"再按一次退出我的一个",Toast.LENGTH_SHORT).show();
            startTime = currentTime;
        }else {
            //停止图片轮播
            handler.obtainMessage(10088).sendToTarget();
            finish();
        }
    }
}
